package org.example.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.system.common.model.Option;
import org.example.system.common.result.PageResult;
import org.example.system.common.result.Result;
import org.example.system.model.form.DictForm;
import org.example.system.model.form.DictTypeForm;
import org.example.system.model.query.DictPageQuery;
import org.example.system.model.query.DictTypePageQuery;
import org.example.system.model.vo.DictPageVO;
import org.example.system.model.vo.DictTypePageVO;
import org.example.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import org.example.system.service.SysDictService;
import org.example.system.service.SysDictTypeService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典接口控制器
 * 处理字典相关的请求，包括字典和字典类型的增删改查
 *
 * @author liuyang
 */
@Tag(name = "06.字典接口")
@RestController
@RequestMapping("/api/v1/dict")
@RequiredArgsConstructor
public class SysDictController {

    private final SysDictService dictService;
    private final SysDictTypeService dictTypeService;

    // 权限常量
    private static final String PERM_DICT_ADD = "sys:dict:add";
    private static final String PERM_DICT_EDIT = "sys:dict:edit";
    private static final String PERM_DICT_DELETE = "sys:dict:delete";
    private static final String PERM_DICT_TYPE_ADD = "sys:dict_type:add";
    private static final String PERM_DICT_TYPE_EDIT = "sys:dict_type:edit";
    private static final String PERM_DICT_TYPE_DELETE = "sys:dict_type:delete";

    /**
     * 获取字典分页列表
     *
     * @param queryParams 查询参数
     * @return 字典分页结果
     */
    @Operation(summary = "字典分页列表")
    @GetMapping("/page")
    public PageResult<DictPageVO> getDictPage(@ParameterObject DictPageQuery queryParams) {
        Page<DictPageVO> result = dictService.getDictPage(queryParams);
        return PageResult.success(result);
    }

    /**
     * 获取字典表单数据
     *
     * @param id 字典ID
     * @return 字典表单数据
     */
    @Operation(summary = "字典数据表单数据")
    @GetMapping("/{id}/form")
    public Result<DictForm> getDictForm(@Parameter(description = "字典ID") @PathVariable Long id) {
        DictForm formData = dictService.getDictForm(id);
        return Result.success(formData);
    }

    /**
     * 新增字典
     *
     * @param dictForm 字典表单数据
     * @return 操作结果
     */
    @Operation(summary = "新增字典")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('" + PERM_DICT_ADD + "')")
    @PreventDuplicateSubmit
    public Result<Void> saveDict(@RequestBody DictForm dictForm) {
        boolean result = dictService.saveDict(dictForm);
        return Result.judge(result);
    }

    /**
     * 修改字典
     *
     * @param id 字典ID
     * @param dictForm 字典表单数据
     * @return 操作结果
     */
    @Operation(summary = "修改字典")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('" + PERM_DICT_EDIT + "')")
    public Result<Void> updateDict(@PathVariable Long id, @RequestBody DictForm dictForm) {
        boolean status = dictService.updateDict(id, dictForm);
        return Result.judge(status);
    }

    /**
     * 删除字典
     *
     * @param ids 字典ID，多个以英文逗号(,)拼接
     * @return 操作结果
     */
    @Operation(summary = "删除字典")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('" + PERM_DICT_DELETE + "')")
    public Result<Void> deleteDict(@Parameter(description = "字典ID，多个以英文逗号(,)拼接") @PathVariable String ids) {
        boolean result = dictService.deleteDict(ids);
        return Result.judge(result);
    }

    /**
     * 获取字典下拉列表
     *
     * @param typeCode 字典类型编码
     * @return 字典下拉选项列表
     */
    @Operation(summary = "字典下拉列表")
    @GetMapping("/{typeCode}/options")
    public Result<List<Option<String>>> listDictOptions(@Parameter(description = "字典类型编码") @PathVariable String typeCode) {
        List<Option<String>> list = dictService.listDictOptions(typeCode);
        return Result.success(list);
    }

    /**
     * 获取字典类型分页列表
     *
     * @param queryParams 查询参数
     * @return 字典类型分页结果
     */
    @Operation(summary = "字典类型分页列表")
    @GetMapping("/types/page")
    public PageResult<DictTypePageVO> getDictTypePage(@ParameterObject DictTypePageQuery queryParams) {
        Page<DictTypePageVO> result = dictTypeService.getDictTypePage(queryParams);
        return PageResult.success(result);
    }

    /**
     * 获取字典类型表单数据
     *
     * @param id 字典类型ID
     * @return 字典类型表单数据
     */
    @Operation(summary = "字典类型表单数据")
    @GetMapping("/types/{id}/form")
    public Result<DictTypeForm> getDictTypeForm(@Parameter(description = "字典类型ID") @PathVariable Long id) {
        DictTypeForm dictTypeForm = dictTypeService.getDictTypeForm(id);
        return Result.success(dictTypeForm);
    }

    /**
     * 新增字典类型
     *
     * @param dictTypeForm 字典类型表单数据
     * @return 操作结果
     */
    @Operation(summary = "新增字典类型")
    @PostMapping("/types")
    @PreAuthorize("@ss.hasPerm('" + PERM_DICT_TYPE_ADD + "')")
    @PreventDuplicateSubmit
    public Result<Void> saveDictType(@RequestBody DictTypeForm dictTypeForm) {
        boolean result = dictTypeService.saveDictType(dictTypeForm);
        return Result.judge(result);
    }

    /**
     * 修改字典类型
     *
     * @param id 字典类型ID
     * @param dictTypeForm 字典类型表单数据
     * @return 操作结果
     */
    @Operation(summary = "修改字典类型")
    @PutMapping("/types/{id}")
    @PreAuthorize("@ss.hasPerm('" + PERM_DICT_TYPE_EDIT + "')")
    public Result<Void> updateDictType(@PathVariable Long id, @RequestBody DictTypeForm dictTypeForm) {
        boolean status = dictTypeService.updateDictType(id, dictTypeForm);
        return Result.judge(status);
    }

    /**
     * 删除字典类型
     *
     * @param ids 字典类型ID，多个以英文逗号(,)分割
     * @return 操作结果
     */
    @Operation(summary = "删除字典类型")
    @DeleteMapping("/types/{ids}")
    @PreAuthorize("@ss.hasPerm('" + PERM_DICT_TYPE_DELETE + "')")
    public Result deleteDictTypes(@Parameter(description = "字典类型ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = dictTypeService.deleteDictTypes(ids);
        return Result.judge(result);
    }
}